package cn.shequren.communityPeople.startup.ui;

import android.Manifest;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.glideConfig.GlideApp;
import com.jz.community.basecomm.routerUtils.RouterCommonUtils;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.utils.BaseImageLoaderUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.basecomm.utils.permission.PermissionUtil;

import butterknife.BindView;
import butterknife.OnClick;
import cn.shequren.communityPeople.R;
import cn.shequren.communityPeople.startup.bean.StartUpAdvInfo;
import cn.shequren.communityPeople.startup.presenter.StartUpPresenter;
import cn.shequren.communityPeople.utils.SPUtils;

/**
 * Created by Tony.Fan on 2018/3/22 17:59
 * <p>
 * 启动页
 */
public class StartUpActivity extends BaseMvpActivity<StartUpView.View, StartUpPresenter> implements StartUpView.View {

    @BindView(R.id.startup_adv_iv)
    ImageView startupAdvIv;
    @BindView(R.id.startup_adv_time_tv)
    TextView startupAdvTimeTv;
    private StartUpCountDownTimer startUpCountDownTimer;

    private String[] linkType = {"1", "2", "3"}; //2商品详情页 1、首页 3 、Html
    private StartUpAdvInfo advInfo;
    private Bundle bundle;

    @Override
    protected int getContentView() {
        return R.layout.activity_startup;
    }

    @Override
    protected void initDatas() {
        checkPermissions();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        checkPermissions();
    }

    @Override
    protected void initView() {
    }

    @Override
    protected StartUpPresenter createPresenter() {
        return new StartUpPresenter(this);
    }

    private boolean isStartUpGuideActivity() {
        if (SPUtils.getInstance().isFirstOpenApp(this)) {
            mPresenter.getStartUpAdvInfo();
            return true;
        }
        if (!SPUtils.getInstance().isFirstOpenApp(this)) {
            SPUtils.getInstance().setFirstOpenApp(this, true);
            startToGuide();
            return false;
        }
        return true;
    }

    @OnClick(R.id.startup_adv_time_tv)
    public void startUpAdvTvClick() {
        stopCountDownTimer();
        startToMainHome();
    }

    @OnClick(R.id.startup_adv_iv)
    public void startUpAdvIVClick() {
        stopCountDownTimer();
        handleJumpHomePage(advInfo);
    }

    private void handleJumpHomePage(StartUpAdvInfo startUpAdvInfo) {

        if (Preconditions.isNullOrEmpty(startUpAdvInfo)) {
            startToMainHome();
            return;
        }

        bundle = new Bundle();

        //直接跳转首页
        if (!Preconditions.isNullOrEmpty(startUpAdvInfo.getLinkType()) && linkType[0].equals(startUpAdvInfo.getLinkType())) {
            startToMainHome();
            return;
        }

        //跳转商品详情页
        if (!Preconditions.isNullOrEmpty(startUpAdvInfo.getLinkType()) && linkType[1].equals(startUpAdvInfo.getLinkType())) {
            bundle.putString("goodsUrl", startUpAdvInfo.getLinkParams());
            startToBindMainHome(bundle);
            return;
        }

        //跳转Html
        if (!Preconditions.isNullOrEmpty(startUpAdvInfo.getLinkType()) && linkType[2].equals(startUpAdvInfo.getLinkType())) {
            bundle.putString("url", startUpAdvInfo.getLinkParams());
            bundle.putString("title", startUpAdvInfo.getTitle());
            startToBindMainHome(bundle);
            return;
        }
    }

    @Override
    public void showStartUpAdvInfo(StartUpAdvInfo startUpAdvInfo) {

        if (startUpAdvInfo == null) {
            startToMainHome();
            return;
        }


        advInfo = startUpAdvInfo;

        if (!Preconditions.isNullOrEmpty(startUpAdvInfo)) {
            handleStartUpAdv(startUpAdvInfo);
        } else {
            startToMainHome();
        }
    }

    private void handleStartUpAdv(StartUpAdvInfo startUpAdvInfo) {
        BaseImageLoaderUtils.getInstance().loadDefaltImage(this, startupAdvIv, startUpAdvInfo.getImage());
        SHelper.vis(startupAdvTimeTv);
        startUpCountDownTimer = new StartUpCountDownTimer(5000, 1000);
        startUpCountDownTimer.start();
    }

    /**
     * 跳转首页
     */
    private void startToMainHome() {
        RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.APP_MODULE_MAIN);
        finish();
    }

    private void startToBindMainHome(Bundle bundle) {
        RouterCommonUtils.startParamsActivity(RouterIntentConstant.APP_MODULE_MAIN, bundle);
        finish();
    }

    /**
     * 跳转引导页
     */
    private void startToGuide() {
        RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.APP_MODULE_STARTUP_GUIDE);
        finish();
    }

    @Override
    public Context getContext() {
        return this;
    }

    class StartUpCountDownTimer extends CountDownTimer {
        public StartUpCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        @Override
        public void onFinish() {
            RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.APP_MODULE_MAIN);
            finish();
        }
        @Override
        public void onTick(long millisUntilFinished) {
            startupAdvTimeTv.setText(millisUntilFinished / 1000 + "s跳转");
        }
    }

    /**
     * 检测申请权限
     */
    private void checkPermissions() {
        PermissionUtil.handleRunTimePermis(this, () -> {
                    isStartUpGuideActivity();
                }, Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION);

    }

    private void stopCountDownTimer() {
        if (startUpCountDownTimer != null) {
            startUpCountDownTimer.cancel();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopCountDownTimer();
    }
}
